<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 店铺名称管理
 *
 * @icon fa fa-circle-o
 */
class Fbchart extends Backend
{
    protected $model = null;
    protected $admin = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('AdminLog');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        $usatime = date('Y-m-d H:i:s',time() - 12*3600);
        $this->view->assign("usatime", $usatime);

//        默认七天搜索
        $begintime = date('Y-m-d 00:00:00', strtotime("-6 day"));
        $endtime= date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1);
        $time_ranking = $begintime.' - '.$endtime;
        $this->view->assign("time_ranking", $time_ranking);
        return $this->view->fetch();
    }

    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign("row", $row->toArray());
        return $this->view->fetch();
    }

    /**
     * example_times
     */
    public function example_times($example_times,$type,$timedate)
    {
        if($type == 1){
            $explode_times = explode(' - ',$example_times);
            $times = $timedate.'>='.strtotime($explode_times[0]).' and '.$timedate.'<='.strtotime($explode_times[1]);
        }elseif ($type == 2){
            switch ($example_times)
            {
//                全部
                case 0:
                    $times = '1=1';
                    break;
//                今日
                case 1:
                    $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
                    $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
                    $times = $timedate.'>='.$beginToday.' and '.$timedate.'<='.$endToday;
                    break;
//                昨日
                case 2:
                    $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
                    $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
                    $times = $timedate.'>='.$beginYesterday.' and '.$timedate.'<='.$endYesterday;
                    break;
//                最近七天
                case 3:
                    $begintime = strtotime(date('Y-m-d 00:00:00', strtotime("-6 day")));
                    $endtime=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
                    $times = $timedate.'>='.$begintime.' and '.$timedate.'<='.$endtime;
                    break;
//                最近三十天
                case 4:
                    $begintime = strtotime(date('Y-m-d 00:00:00', strtotime("-29 day")));
                    $endtime=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
                    $times = $timedate.'>='.$begintime.' and '.$timedate.'<='.$endtime;
                    break;
//                本月
                case 5:
                    $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
                    $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
                    $times = $timedate.'>='.$beginThismonth.' and '.$timedate.'<='.$endThismonth;
                    break;
//                上个月
                case 6:
                    $lastmonth_start=mktime(0,0,0,date('m')-1,1,date('Y'));
                    $lastmonth_end=mktime(23,59,59,date('m'),0,date('Y'));
                    $times = $timedate.'>='.$lastmonth_start.' and '.$timedate.'<='.$lastmonth_end;
                    break;
                default:
                    $times = '1=1';
            }
        }else{
            $times = '1=1';
        }

        return $times;
    }

    public function ajax_roi()
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $example_times = $this->request->post("example_times");
        $time = $this->request->post("time");
        $admin_id = $this->request->post("admin_id");
        //$time优先级最高
        if(!empty($time)){
            $wheretime = $this->example_times($time,1,'timedate');
        }else{
            $wheretime = $this->example_times($example_times,2,'timedate');
        }
        $return = array();
        $this->success('返回数据','', $return);
    }
    public function ajax_net_profit()
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $example_times = $this->request->post("example_times");
        $time = $this->request->post("time");
        $admin_id = $this->request->post("admin_id");
        //$time优先级最高
        if(!empty($time)){
            $wheretime = $this->example_times($time,1,'timedate');
        }else{
            $wheretime = $this->example_times($example_times,2,'timedate');
        }
        $return = array();
        $this->success('返回数据','', $return);
    }
    public function ajax_sales()
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $example_times = $this->request->post("example_times");
        $time = $this->request->post("time");
        //$time优先级最高
        if(!empty($time)){
            $wheretime = $this->example_times($time,1,'timedate');
        }else{
            $wheretime = $this->example_times($example_times,2,'timedate');
        }

//        $where = 'and admin_id = '.$this->admin['id'];
//        $sql = "SELECT FROM_UNIXTIME(timedate,'%m-%d') as timedate,SUM(advertising) as advertisingz,SUM(sales) as salesz,SUM(single_quantity) as single_quantityz FROM fa_fedition_report WHERE ".$wheretime." ".$where." GROUP BY timedate";
//        $statistics = \think\Db::query($sql);
//        dump($statistics);
//        die;

        $return = array();
        $this->success('返回数据','', $return);
    }
    public function ajax_ranking()
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $example_times = $this->request->post("example_times");
        $time = $this->request->post("time");
        $admin_id = $this->request->post("admin_id");
        //$time优先级最高
        if(!empty($time)){
            $wheretime = $this->example_times($time,1,'timedate');
        }else{
            $wheretime = $this->example_times($example_times,2,'timedate');
        }
        $return = array();
        $this->success('返回数据','', $return);
    }
}
